package com.mandao.oneplat.boot;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;

/**
 * Created by sunson on 2017/3/20.
 */
public final class OnePlatBoot {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public OnePlatBoot(OnePlatConfig oneOption) throws Throwable {
        logger.info("启动应用服务，参数：{}", ToStringBuilder.reflectionToString(oneOption));
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.option(ChannelOption.SO_BACKLOG, 1024);
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new OnePlatBootChannel(oneOption));
            String localip = InetAddress.getLocalHost().getHostAddress();
            logger.info("应用服务启动：{}:{}", localip, oneOption.getHttpPort());
            Channel ch = b.bind(InetAddress.getLocalHost().getHostAddress(), oneOption.getHttpPort()).sync().channel();
            ch.closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Throwable {
        OnePlatConfig oneOption = new OnePlatConfig();
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("--h")) {
                System.out.println("启动说明");
                System.out.println("java -jar oneplat.jar 启动参数");
                System.out.println("启动参数如下：");
                System.out.println("--p              应用启动端口，默认[8080]");
                System.out.println("--base-dir       应用根目录");
                System.out.println("--responsive     是否为响应式，默认[false]，如果否，则移动端加载模板目录为theme/mobile");
                System.out.println("--tpl-cache      模板是否缓存，默认[false]，开发模式下请不要使用");
                System.out.println("--base-packages  应用服务端扫描路径，IOC自动扫描注解用");
                System.out.println("--mongo-host     MongoDB地址，默认[127.0.0.1]");
                System.out.println("--mongo-port     MongoDB端口，默认[27017]");
                System.out.println("--mongo-db       MongoDB库，默认[OnePlat]");
                System.exit(0);
            } else if (args[i].equals("--p")) {
                oneOption.setHttpPort(Integer.parseInt(args[++i]));
            } else if (args[i].equals("--base-dir")) {
                oneOption.setBaseDir(args[++i]);
            } else if (args[i].equals("--responsive")) {
                oneOption.setResponsiveMode(true);
            } else if (args[i].equals("--tpl-cache")) {
                oneOption.setTemplateCacheable(Boolean.valueOf(args[++i]));
            } else if (args[i].equals("--base-packages")) {
                oneOption.setBasePackages(args[++i]);
            } else if (args[i].equals("--mongo-host")) {
                oneOption.setMongoHost(args[++i]);
            } else if (args[i].equals("--mongo-port")) {
                oneOption.setMongoPort(Integer.parseInt(args[++i]));
            } else if (args[i].equals("--mongo-db")) {
                oneOption.setMongoDb(args[++i]);
            }
        }
        new OnePlatBoot(oneOption);
    }
}
